<template>
  <a-modal
    title="新增客户"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="客户名称"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input placeholder="请输入客户名称" v-decorator="['name', {rules: [{required: true, message: '请输入客户名称！'}]}]" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="联系人"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input placeholder="请输入联系人" v-decorator="['contacts']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="电话"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input placeholder="请输入电话" v-decorator="['tel']" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="邮编"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input placeholder="请输入邮编" v-decorator="['zipCode']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="税号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input placeholder="请输入税号" v-decorator="['taxNo']" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="开户行"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input placeholder="请输入开户行" v-decorator="['bank']" />
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="银行卡号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-input placeholder="请输入银行卡号" v-decorator="['bankCard']" />
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              label="是否启用"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-select style="width: 100%" placeholder="请选择是否启用" v-decorator="['isEnable', {rules: [{ required: true, message: '请选择是否启用！' }], initialValue: '1'}]">
                <a-select-option v-for="(item,index) in isEnableData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24" style="margin-right: -69px;">
            <a-form-item
              label="地址"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-cascader
                v-decorator="['regionAddr', {rules: [{required: false, message: '请选择省/市/区'}]}]"
                :options="cityTreeData"
                @change="handleChangeCity"
                placeholder="请选择省/市/区"
                :fieldNames="{ label: 'label', value: 'code', children: 'children' }"/>
            </a-form-item>
          </a-col>
          <a-col :md="12" :sm="24">
            <a-form-item
              :colon="false"
              :labelCol="{span:0}"
              :wrapperCol="{span:24}"
              has-feedback
            >
              <a-input placeholder="请输入详细地址" v-decorator="['address', {rules: [{required: false, message: '请输入地址！'}]}]"/>
            </a-form-item>
          </a-col>
        </a-row>
        <a-row :gutter="24">
          <a-col :md="12" :sm="24">
            <a-form-item
              label="备注"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback
            >
              <a-textarea placeholder="请输入备注" v-decorator="['remark', {rules: [{required: false, message: '请输入备注！'}]}]" :auto-size="{ minRows: 2, maxRows: 6 }"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { customerAdd } from '@/api/modular/main/base/customer/customerManage'
  import { cityTree } from '@/api/modular/main/base/city/cityManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        cityTreeData: [],
        isEnableData: [],
        regions: []
      }
    },
    created () {
      cityTree().then((res) => {
         this.cityTreeData = res.data
      })
    },
    methods: {
      // 选择省市区
      handleChangeCity (value, selectedOptions) {
        this.regions = selectedOptions
      },
      // 初始化方法
      add (record) {
        this.visible = true
        const isEnableOption = this.$options
        this.isEnableData = isEnableOption.filters['dictData']('whether')
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
            }
            if (this.regions != null && this.regions.length === 3) {
              values['provinceCode'] = this.regions[0].code
              values['province'] = this.regions[0].label
              values['cityCode'] = this.regions[1].code
              values['city'] = this.regions[1].label
              values['areaCode'] = this.regions[2].code
              values['area'] = this.regions[2].label
            }
            customerAdd(values).then((res) => {
              if (res.success) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('新增失败:' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
